package top.fzqblog.firstapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.fzqblog.fragment.TabLayoutFragment;

/**
 * Created by 抽离 on 2017/9/18 0018.
 */

public class TabLayoutTest extends FragmentActivity {

    @BindView(R.id.tab_layout_test_tab)
    public TabLayout tabLayout;

    @BindView(R.id.tab_layout_test_viewpager)
    public ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_layout_test);
        ButterKnife.bind(this);
        viewPager.setAdapter(new TabLayoutVPAdapter(getSupportFragmentManager(),     new ArrayList<Integer>(){
            {
               add(1);
               add(2);
               add(3);
               add(4);
            }
        }, new ArrayList<String>(){
            {
                add("tab1");
                add("tab2");
                add("tab3");
                add("tab4");
            }
        }));

        tabLayout.setupWithViewPager(viewPager);//显示Tab title
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
}

class TabLayoutVPAdapter extends FragmentPagerAdapter {

    public List<Integer> pageList;

    public List<String> titles;

    public TabLayoutVPAdapter(FragmentManager fm, List<Integer> pageList, List<String> titles) {
        super(fm);
        this.pageList = pageList;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return TabLayoutFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        return pageList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}